/**
 *
 * Common.js
 * 此js文件储存了当前状态最基本的信息，包括：当前的企业信息，当前的用户信息。
 *
 */
import {PLATFORM_KEY} from "../../views/router";
import {cachedGet} from "./Cache";

const PLATFORM_URL = '/unitPlatform/getUnitPlatformByCode';

export function getPlatformCode(){
    let name = '';
    if(!this||!this.$route){
        let path = location.pathname;
        if(path.startsWith('/')){
            path = path.slice(1);
        }
        const arr = path.split('/');
        if(arr&&arr.length>0){
            name = arr[0];
        }else{
            throw new Error("平台名称解析失败");
        }
    }else{
        name = this.$route.params[PLATFORM_KEY];
    }
    if(!name||name.length<=0){
        throw new Error("无法获取到平台名");
    }
    return name;
}

async function loadPlatform() {
    let name = getPlatformCode();
    try {
        const response = await cachedGet(`${PLATFORM_URL}/${name}`,{cache:true,params:{}});
        return response.data.parameter;
    }catch (e) {
        console.error("平台信息加载失败。")
        throw e;
    }
}

/**
 * 获取当前的企业信息
 */
export async function getCurrentPlatform() {
    return await loadPlatform.call(this);
}
